package top.pmwly.oauth2_jwt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName SpringSecurityConfig.java
 * @Description Security配置类
 * @createTime 2021年08月23日 14:36:00
 */
@EnableWebSecurity
@Configuration
public class SpringSecurityConfig  extends WebSecurityConfigurerAdapter {

    /**
     * 注入密码解析加密
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * 注入认证管理器
     */
    @Bean
    public AuthenticationManager authenticationManager1() throws Exception {
        return super.authenticationManager();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf防护
        http.csrf().disable()
                .authorizeRequests()
                //这些uri下的方法不需要授权
                .antMatchers("/login/**","/autho/**","/logout/**")
                .permitAll()
                //其他方法需要授权
                .anyRequest()
                .authenticated()
                .and()
                //登录表单的请求放下
                .formLogin().permitAll();
    }
}
